package com.dingding.service.user.core.biz.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dto.AbstractResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.SystemMessage;
import com.dingding.common.core.config.Constants;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.facade.user.dto.message.system.GetSystemMessageEnum;
import com.dingding.facade.user.dto.message.system.ReqGetSystemMessage;
import com.dingding.facade.user.dto.message.system.ResGetSystemMessage;
import com.dingding.facade.user.dto.message.system.SystemMessageDto;
import com.dingding.service.user.core.biz.IGetSystemMessageService;
import com.dingding.service.user.core.dao.ISystemMessageDao;

/**
 * @Description:获取系统信息服务实现
 * @author fengshuonan
 * @date 2016年7月25日 下午5:12:53
 * @version 叮叮搭车
 * @since 2.3.2
 */
@Component("getSystemMessageService")
public class GetSystemMessageServiceImpl extends AbstractResponse<ResGetSystemMessage, ReqGetSystemMessage> implements IGetSystemMessageService {

	@Resource(name = "systemMessageDao")
	private ISystemMessageDao systemMessageDao;
	
	@Override
	@Transactional(readOnly = true)
	public String getSystemMessageService(String requestData) {
		
		ResponseEntity<ResGetSystemMessage> responseEntity = new ResponseEntity<ResGetSystemMessage>();
		ReqGetSystemMessage req = super.parseRequestJson(requestData);
		
		//验证参数
		if(!super.validateParam(responseEntity, req)){
			return super.responseBeanToJson(responseEntity);
		}
		
		//查询系统消息
		responseEntity = getSystemMsgList(responseEntity,req);
		
		return super.responseBeanToJson(responseEntity,"yyyy-MM-dd HH:mm");
	}
	
	
	/**
	 * @Description:获取系统信息列表
	 * @author fengshuonan
	 * @since 2.3.2
	 */
	private ResponseEntity<ResGetSystemMessage> getSystemMsgList(ResponseEntity<ResGetSystemMessage> responseEntity, ReqGetSystemMessage req) {
		
		List<SystemMessage> list = this.systemMessageDao.getSystemMsgList(req.getUserId(), req.getPageIndex());
		
		if(list == null || list.size() == 0){
			responseEntity.setCode(GetSystemMessageEnum.OK.getCode());
			responseEntity.setErrorMessage(GetSystemMessageEnum.OK.getMessage());
			responseEntity.setPromptMessage(GetSystemMessageEnum.OK.getMessage());
			return responseEntity;
		}
		
		// 广播列表
		List<SystemMessageDto> newList = new ArrayList<SystemMessageDto>();
		// 广播Id列表
		String broadCastIdList[] = new String[list.size()];
		ResGetSystemMessage resGetSystemMessage = new ResGetSystemMessage();
		
		int currentPage = Constants.MAX_SYSTEM_LIST_SIZE * (req.getPageIndex() - 1);
		
		int size = Constants.MAX_SYSTEM_LIST_SIZE;
		
		// 广播列表
		for (int i = currentPage ; i < list.size(); i++) {
			
			if(i == currentPage + size){
				break;
			}
			
			SystemMessageDto dto = new SystemMessageDto();
			copyAttribute(list.get(i),dto);
			newList.add(dto);
		}
		
		for (int i = 0; i < list.size(); i++) {
			broadCastIdList[i] = list.get(i).getSmId();
		}
		
		resGetSystemMessage.setMessageList(newList);
		resGetSystemMessage.setMessageIdList(broadCastIdList);
		
		responseEntity.setCode(GetSystemMessageEnum.OK.getCode());
		responseEntity.setErrorMessage(GetSystemMessageEnum.OK.getMessage());
		responseEntity.setPromptMessage(GetSystemMessageEnum.OK.getMessage());
		responseEntity.setResponseBody(resGetSystemMessage);
		return responseEntity;
	}
	
	
	/**
	 * @Description:属性拷贝
	 * @author fengshuonan
	 * @return void
	 * @since 2.3.2
	 */
	private void copyAttribute(SystemMessage systemMessage,SystemMessageDto dto){
		BeanUtils.copyProperties(systemMessage, dto);
		dto.setMessageId(systemMessage.getSmId());
		dto.setTimeStamp(DateFormatUtil.getTimeInMillisByTime(dto.getCreateTime()));
	}
}	
